<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html lang="zh">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
  <title>重置密码</title>
  <link rel="icon" href="favicon.ico" type="image/ico">
  <meta name="keywords" content=" ">
  <meta name="description" content=" ">
  <meta name="author" content="yinqi">
  <link href="${pageContext.request.contextPath}/static/css/bootstrap.min.css" rel="stylesheet">
  <link href="${pageContext.request.contextPath}/static/css/materialdesignicons.min.css" rel="stylesheet">
  <link href="${pageContext.request.contextPath}/static/css/style.min.css" rel="stylesheet">
</head>

<body>
<div class="container-fluid p-t-15">

  <div class="row">
    <div class="col-lg-12">
      <div class="card">
        <div class="card-body">

          <form method="post" class="site-form">
            <div class="form-group">
              <label for="loginname">登录名</label>
              <input type="text" class="form-control" name="loginname" id="loginname" placeholder="输入登录名"><span id="namemsg"></span>
            </div>
            <div class="form-group">
              <label for="newpwd">新密码</label>
              <input type="password" class="form-control" name="newpwd" id="newpwd" placeholder="输入新的密码">
            </div>
            <div class="form-group">
              <label for="repwd">确认新密码</label>
              <input type="password" class="form-control" name="repwd" id="repwd" placeholder="请确认新密码">
            </div>
            <div class="form-group">
              <label for="email">邮箱</label>
              <input type="text" class="form-control" name="email" id="email" value="" placeholder="请输入账户绑定邮箱"/>
            </div>
            <div class="form-group">
              <label for="vcode">输入邮箱验证码</label>
              <div style="float:left;display:flex;width:100%">
                <input type="text" class="form-control" name="vcode" id="vcode" placeholder="请输入邮箱验证码" style="width: 60%">
                <input class="btn btn-info col-lg-2" id="btn" name="btn" value="发送验证码" onclick=" " readonly="readonly" style="width: 30%"/>
              </div>
            </div>
            <input type="hidden" id="ecode" name="ecode" value="ecode"/>
            <input type="hidden" id="mail" name="mail" value="mail"/>
            <button class="btn btn-primary" style="margin-top: 10px" onclick="forpwd()">重置密码</button>
          </form>

        </div>
      </div>
    </div>

  </div>

</div>

<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/jquery.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/bootstrap.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/main.min.js"></script>
<script>
  $(function () {
    $("#loginname").blur(function () {
      var loginname=$(this).val();
      if (loginname == "") {
        alert("请输入登录名");
        return false;
      }
      $.ajax({
        type: "post",
        url: "${pageContext.request.contextPath}/user/checkeml",
        data: "loginname="+loginname,
        success:function (codes) {
          console.log(codes);
          if (codes=='no'){
            $("#namemsg").text("用户不存在").css("color","red").css("font-size","10px");
          }else{
            $("#namemsg").text("用户存在").css("color","green").css("font-size","10px");
            $("#mail").attr("value",codes);
          }
        }
      });
    });
  });

  $(function () {
    $("#btn").click(function () {
      var mail=$("#mail").val();
      var email=$("#email").val();
      var reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;   //验证邮箱正则表达式

      if (email == "") {
        alert("请输入邮箱！");
        return false;
      }
      if (!reg.test(email)){
        alert("邮箱格式不正确");
        return false;
      }
      if (mail != email){
        alert("该用户邮箱不正确");
        return false;
      }
      $.ajax({
        type: "post",
        url: "${pageContext.request.contextPath}/user/emailcod",
        data: "email="+email,
        success:function (codes) {
          document.getElementById("btn").value="已发送";
          // $("#btn").attr("value","已发送");
          $("#ecode").attr("value",codes);
        }
      });
    });
  });


  function forpwd() {
    var loginname=$("#loginname").val();
    var newpwd=$("#newpwd").val();
    var repwd=$("#repwd").val();
    var vcode=$("#vcode").val();
    var ecode=$("#ecode").val();
    // var userData = $("#userForm").serialize();
    if (newpwd == ""){
      alert("请输入新密码！");
      return false;
    }
    if (newpwd != repwd) {
      alert("两次密码不一致！");
      return false;
    }
    if (vcode == ""){
      alert("请输入验证码！");
      return false;
    }
    $.ajax({
      url :  "${pageContext.servletContext.contextPath}/user/forpwd",
      data:{"loginname":loginname,"newpwd":newpwd,"vcode":vcode,"ecode":ecode},
      // data : userData,
      type : "post",
      success (msg) {
        if (msg == 'suc') {
          //跳转到登录
          alert("重置成功,请登录");
          window.location.href = "${pageContext.servletContext.contextPath}/user/tologin";
        }
        if (msg=='error'){
          alert("验证码有误，请重新申请");
        }
      },
    });
  }
</script>
</body>
</html>